export function usedTimeShow(timer: string | number) {
    if (!timer) {
        return '';
    }
    let timerNum = 0;
    if (typeof timer === "string") {
        timerNum = parseInt(timer) / 1000;
    }
    if (typeof timer === "number") {
        timerNum = timer / 1000;
    }
    let tips = '';
    switch (true) {
        case timerNum === 0:
            break;
        case timerNum < 60:
            tips = parseInt(String(timerNum)) + '秒';
            break;
        case timerNum >= 60 && timerNum < 3600:
            tips = parseInt(String(timerNum / 60)) + '分钟';
            break;
        case timerNum >= 3600 && timerNum < 86400:
            tips = parseInt(String(timerNum / 3600)) + '小时';
            break;
        case timerNum >= 86400 && timerNum < 2592000:
            tips = parseInt(String(timerNum / 86400)) + '天';
            break;
        default:
            if (timerNum >= 2592000 && timerNum < 365 * 86400) {
                tips = parseInt(String(timerNum / (86400 * 30))) + '个月';
            } else {
                tips = parseInt(String(timerNum / (86400 * 365))) + '年';
            }
    }
    return tips;
}

export function DateFormat(date: number | Date, fmt: string | undefined) {
    if (date === null) {
        return null;
    }
    if (!date) {
        date = new Date();
    }
    if (!(date instanceof Date)) {
        date = new Date(date);
    }
    if (!fmt) {
        fmt = 'yyyy-MM-dd hh:mm:ss';
    }
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substring(4 - RegExp.$1.length))
    }

    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    };
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            // @ts-ignore
            let str = `${o[k]}`;
            fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : ('00' + str).substring(str.length))
        }
    }
    return fmt
}